"""
这个文件用来读取train_acc.txt，然后画acc和loss曲线
"""
import matplotlib.pyplot as plt
import re
import config as cfg
txt_path = cfg.train_infor  #train_acc.txt文件路径
txt_file = open(txt_path,'r')
tra_epochs,tra_loss,tra_lr,val_loss = [],[],[],[]
for line in txt_file.readlines()[29:]:
    if line[0] == 'E':
        numbers = list(map(float,re.findall(r'\d+\.?\d*',line)))

        epoch = int(numbers[0])
        train_lr = numbers[1]
        train_loss = numbers[2]
        valid_loss = numbers[3]

        tra_epochs.append(epoch)
        tra_loss.append(train_loss)
        tra_lr.append(train_lr)
        val_loss.append(valid_loss)
txt_file.close()

#------------------------画图--------------------------------------
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 10), dpi=100)
axs[0].plot(tra_epochs,tra_lr,c='green')
axs[0].set_xlabel('epoch')
axs[0].set_ylabel('lr')
axs[0].set_title('lr')

axs[1].plot(tra_epochs,tra_loss,c='blue',label='loss')
axs[1].set_xlabel('epoch')
axs[1].set_ylabel('train_loss')
axs[1].set_title('train_loss')

axs[2].plot(tra_epochs,val_loss,c='red',label='acc')
axs[2].set_xlabel('epoch')
axs[2].set_ylabel('val_loss')
axs[2].set_title('val_loss')
plt.show()
